<?php

    ##
    # Makes SQL from xml
    class SQLBuilder {

        public $xml;

        function SQLBuilder($xml){
            $this->xml = $xml;
            $this->SQL = $this->Build($this->xml,"all",false);
        }

        function Build($xml,$show,$checkIfNotExists) {
            if ($checkIfNotExists)
                $checkIfNotExists = 'IF NOT EXISTS';
            else
                $checkIfNotExists = '';
            $FieldDefaults=" NOT NULL,";
            $TableDefaults=" ENGINE=MyISAM DEFAULT CHARSET=utf8;";
            foreach ($xml->table as $tables){
                $show=="all"?$que="all":$que=$tables['name'];
                if ($show==$que){
                    $ret.= "CREATE TABLE {$checkIfNotExists} `{$tables['name']}` (";
                    $ret.= "\n`ID` int(11) NOT NULL AUTO_INCREMENT,";
                    foreach ($tables->field as $fields){
                        $ret.="\n`{$fields['name']}`";
                        switch ($fields["type"]) {
                            case "text":
                            case "date":
                                $ret.=" {$fields['type']}";
                            break;
                            case "char":
                                $ret.=" varchar({$fields['size']})";
                            break;
                            case "set":
                                $values=split(',',$fields['values']);
                                $ret.=" set(";
                                foreach ($values as $val) {
                                    $temp=split(':',$val);
                                    $value=$temp[0];
                                    $ret.="'$value',";
                                }
                                $fields<>"" ? $cc="default '$fields'":$cc="";
                                $ret=trim($ret,',');
                                $ret.=") $cc";
                            break;
                        }
                        $ret.=$FieldDefaults;
                    }
                $ret = trim($ret,',').",\nPRIMARY KEY(`ID`) \n)".$TableDefaults."\n";
                }
                $ret_array[]=$ret;
                $ret="";
            }
            foreach ($xml->record as $records){
                $insertQuery = "INSERT INTO `{$records['table']}` VALUES ('',";
                foreach ($records->node as $nodes)
                    $insertQuery.= "\n'{$nodes}',";
                $insertQuery = trim($insertQuery, ',').");\n";
                $ret_array[]=$insertQuery;
            }
            return $ret_array;
        }

        function GetSQL($show="all",$checkIfNotExists=false){
            if ($show<>"all")
                $this->SQL=$this->Build($this->xml,$show,$checkIfNotExists);
            return $this->SQL;
        }

        function ExecSQL($Vezir) {
            $state = true;
            foreach ($this->SQL as $sql)
                if($state)
                    $state = $Vezir->ExecuteQuery($sql);
            return $state;
        }

    }

?>
